package com.young.springdemo.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.young.springdemo.AsyncService.StudentServiceAsync;
import com.young.springdemo.config.annotation.LogOperateType;
import com.young.springdemo.config.annotation.OperateLog;
import com.young.springdemo.controller.param.UserBase;
import com.young.springdemo.jpa.entity.TeacherEntity;
import com.young.springdemo.service.feign.FeignService;
import com.young.springdemo.service.jpa.TeacherService;
import com.young.springdemo.utils.DemoResp;
import com.young.springdemo.utils.ResponseData;
import com.young.springdemo.utils.domain.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/studentsController")
@Slf4j
public class StudentsController {

    @Autowired
    private StudentServiceAsync studentServiceAsync;

    @Autowired
    private TeacherService teacherService;
    @GetMapping("getInt")
    @OperateLog(desc = "操作",operateType = "查询")
    public int getInt(){
        log.info("开始执行getInt");
        return 14;
    }

    @GetMapping("getAnnotentionParam")
//    @HandleParam(notNull =true ,notBlank = true)
    public void getAnnotentionParam(String name,int age,String sex){
        studentServiceAsync.getTest(name,age+"",sex);
    }

    @RequestMapping(value = "getStu", method = RequestMethod.GET)
    @ResponseBody
    public void getStudent() {
        log.info("开始执行接口");
        for (int i = 0; i < 5; i++) {
            studentServiceAsync.getStudent();
            studentServiceAsync.getStu();
        }
        log.info("结束执行接口");
    }

    @RequestMapping(value = "testPy", method = RequestMethod.GET)
    @ResponseBody
    @com.young.springdemo.config.log.OperateLog(operate = LogOperateType.QUERY,desc = "student测试空接口")
    public Result testPy() {
//        try {
            List<String> result = new ArrayList<>();
//            String s = result.get(2);
//            System.out.println(s);
            return Result.success();
//        }catch (Exception e){
//            log.error("Exception [{}]",e.getMessage());
//            return Result.error(CodeMsg.SERVER_EXCEPTION);
//        }
    }

    @PostMapping("getAllTeachers")
    @com.young.springdemo.config.log.OperateLog(operate = LogOperateType.QUERY,desc = "学生类测试空参数")
    public Result getAllTeachers(@RequestBody UserBase base) {
        log.info("用户[{}]开始查询所有信息", base.toString());
        List<TeacherEntity> all = teacherService.findAll(base.getUserName());
        log.info("获取到的教师用户有[{}]个", all.size());
        return Result.success(all);
    }




/*
    public static void main(String[] args) {
        String line;
        try {
            //创建读取python的必要条件.
            // 数组1:必须是"python",指定文件格式
            //数组2:该python的文件的路径,可以是绝对路径,也可以是相对路径
            //数组3及以后:是对python调用的方式的传递参数的方式
            String[] params = new String[]
                    {"python", "D:\\py\\project\\hello_py.py", "小明", "20"};
            //给调用python分配一个进程,并执行py文件
            Process process = Runtime.getRuntime().exec(params); //执行py文件
            //读取python写的数据
            BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream(), "GBK"));
            while ((line = in.readLine()) != null) {
                //todo 输出处理python传输数据
                System.out.println(line);
            }
            in.close();//关闭字节流
            //java代码中的process.waitFor()返回值为0表示我们调用python脚本成功，
            //返回值为1表示调用python脚本失败
            int re = process.waitFor();//0:执行成功;1:执行失败;2:调用失败
            switch (re) {
                case 0:
                    //todo :执行成功
                    System.out.println("调用python,执行成功");
                    break;
                case 1:
                    //todo :执行失败
                    System.out.println("调用python,执行失败");
                    break;
                case 2:
                    //todo: 调用失败
                    System.out.println("调用python文件失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

*/

    //测试Feign


    @Autowired
    private FeignService feignClientProxy;

    @GetMapping(value = "test")
    @ResponseBody
    public String testFeign(@RequestParam(name = "name") String name) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        System.out.println("开始执行");
        ResponseData<String> respResponseBody = feignClientProxy.invoke();
        System.out.println("执行结束");
        return "调用结果:"+ objectMapper.writeValueAsString(respResponseBody);
    }

    @RequestMapping(value = "getParam",method =RequestMethod.GET)
    @ResponseBody
    public String getParam(@RequestParam(name = "name") String name) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        System.out.println("开始执行");
        String obj = feignClientProxy.getParam(name,"12");
        System.out.println("执行结束");
        return "调用结果:"+ objectMapper.writeValueAsString(obj.toString());
    }

    @RequestMapping(value = "getPost",method =RequestMethod.POST)
    @ResponseBody
    public String getPost() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        System.out.println("开始执行");
        String obj = feignClientProxy.getPost();
        System.out.println("执行结束");
        return "调用结果:"+ objectMapper.writeValueAsString(obj.toString());
    }

    @RequestMapping(value = "getPostParam",method =RequestMethod.POST)
    @ResponseBody
    public String getPostParam(@RequestParam(name = "name") String name) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        System.out.println("开始执行");
        String obj = feignClientProxy.getPostParam(name);
        System.out.println("执行结束");
        return "调用结果:"+ objectMapper.writeValueAsString(obj.toString());
    }


    @RequestMapping(value = "getPostParamResult",method =RequestMethod.POST)
    @ResponseBody
    public String getPostParamResult(@RequestParam(name = "name") String name) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResponseData<DemoResp> postParamResult = feignClientProxy.getPostParamResult(name);

        System.out.println("执行结束,"+postParamResult.toString());
        return "调用结果:"+ objectMapper.writeValueAsString(postParamResult.toString());
    }

    @RequestMapping(value = "getPostResult",method =RequestMethod.POST)
    @ResponseBody
    public String getPostResult(@RequestParam(name = "name") String name) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        System.out.println("开始执行");
        ResponseData<DemoResp> postParamResult = feignClientProxy.getPostResult();
        System.out.println("执行结束,"+postParamResult.toString());
        return "调用结果:"+ objectMapper.writeValueAsString(postParamResult.toString());
    }

    @RequestMapping(value = "getPostParamEntityResult",method =RequestMethod.POST)
    @ResponseBody
    public ResponseData getPostParamEntityResult(@RequestParam(name = "name") String name) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        DemoResp demoResp = new DemoResp();
        demoResp.setName(name);
        ResponseData<DemoResp> postParamResult = feignClientProxy.getPostParamEntityResult(demoResp);
        return postParamResult;
//        System.out.println("执行结束,"+postParamResult.toString());
//        return "调用结果:"+ objectMapper.writeValueAsString(postParamResult.toString());
    }

//    @HandleParam(notNull = true,notBlank = true)
//    public Object getTest(String name,String age,String sex) {
//        System.out.println(name+age+sex);
//        return "success";
//    }
}
